package tq.com.upload;

import java.util.HashMap;

public class TQUploadFileManager {
    private static TQUploadFileManager sUploadFileManager = new TQUploadFileManager();
    private HashMap<String, TQUploadFileTask> mTaskHashMap = new HashMap<>();
    private static String uppload;
    private static String token;

    static public TQUploadFileManager getUploadFileManager(){
        return sUploadFileManager;
    }
    static public void setUploadUrl(String pUrl){
        uppload = pUrl;
    }
    static public void setToken(String pToken){
        token = pToken;
    }

    private void addTask(String file, TQUploadFileTask pTask){
        synchronized (mTaskHashMap){
            mTaskHashMap.put(file, pTask);
        }
    }
    private void remove(String file){
        synchronized (mTaskHashMap){
            mTaskHashMap.remove(file);
        }
    }
    static public class Build{
        private String url;
        private String token;
        private String file;
        private IUploadFileListener mUploadFileListener;
        private TQUploadFileTask mTQUploadFileTask;

//        public Build setUploadUrl(String pUrl){
//            url = pUrl;
//            return this;
//        }
        public Build setToken(String pToken){
            token = pToken;
            return this;
        }
        public Build setFile(String pFile){
            file = pFile;
            return this;
        }

        public Build setListener(IUploadFileListener pListener){
            mUploadFileListener = pListener;
            return this;
        }

        public void start(){
            if(getUrl() == null ||
                    getFile() == null || getUploadFileListener() == null){
                return;
            }

            TQUploadFileTask lTQUploadFileTask = new TQUploadFileTask(this);
            getUploadFileManager().addTask(file, lTQUploadFileTask);
            lTQUploadFileTask.execute();
            mTQUploadFileTask = lTQUploadFileTask;
        }
        public void cancel(){
            mTQUploadFileTask.cancel(true);
            getUploadFileManager().remove(file);
        }

        public String getUrl() {
            if(url == null){
                return uppload;
            }
            return url;
        }

        public String getToken() {
            if(token == null){
                return TQUploadFileManager.token;
            }
            return token;
        }

        public String getFile() {
            return file;
        }
        public IUploadFileListener getUploadFileListener(){
            return mUploadFileListener;
        }

    }

}
